home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 096 / cleanup.lqr / CLEANUP.C next >
Encoding:
C/C++ Source or Header  |  1985-06-03  |  10.4 KB  |  345 lines

  1. /* CLEANUP is a utility program to facilitate the maintanence of the
  2.    USERS file for systems running RBBS-PC CPC12.2 versions B and on.
  3.  
  4.    This program is hereby donated to the public domain as long as these
  5.    comments and source code are not removed or modified in any way.
  6.  
  7.    Donated by: DSOFT
  8.    Author:     William K. Davies
  9.                8990 19th Street #294
  10.                Alta Loma, Ca 91701
  11.    Date:       October 2, 1984
  12.    Updated:    November 25, 1984 -- Add capability to specify USERS filename.
  13.    Phone:      (714)989-6706 <-- Data line running RBBS-PC (300/450/1200)
  14.  
  15.    Although this program is distributed free of charge, a contribution will
  16.    be accepted (suggested $15) for the further development of similar 
  17.    software utilities.
  18.  
  19. */
  20.  
  21. #include "stdio.h"              /* standard I/O routines */
  22.  
  23. /* define external assembly language routines */
  24. extern int scr_cls(), scr_clr(), scr_rowcol(), strncmp(), strlen();
  25. extern int toupper(), gets(), atoi();
  26.  
  27. /* define data storage */
  28. FILE *fp;
  29. char dis_buffer[129], filename[13];
  30. char name[32], password[16], city_state[25];
  31. char machine[20], time_date[15], last_dir[4];
  32. char error;
  33. int security, op1, op2, op3 ,op4, op5, op6, op7, downloads, uploads, elapsed;
  34.  
  35. main()
  36. {
  37.     screen();                                 /* display title screen */
  38.     scr_clr();                                /* clear screen         */
  39.  
  40.     crt(20,1,"Enter name of USERS file to cleaned up: ");
  41.     gets(&filename);
  42.     if ((fp = fopen(filename, "r")) == NULL) {  /* open users file */
  43.        printf("\n Users file %s not found!\n\n", filename); 
  44.     exit(0);
  45.     }
  46.  
  47.     search();                                 /* call main routine    */
  48.     close(fp);                                /* close users file     */
  49.     crt(0, 0, "CLEANUP Done. Have a good day!");
  50. }   /* program ends here */
  51.  
  52. /* routine to search for a user. (Only characters input are compared) */
  53.  
  54. search()
  55. {
  56.     char input[200], again, compare;
  57.     int record;
  58.  
  59.     do {
  60.        crt(20,1,"User Name To Search For: ");
  61.        gets(&input);                         /* get user name */
  62.        cnvrt_string(input);                  /* convert to upper case */
  63.        error = rewind(fp);                   /* reset file pointer */
  64.        record = 0;                           /* reset record counter */
  65.        do {
  66.           again = 1;                         /* set repeat flag       */ 
  67.           get_record();                      /* read disk record      */        
  68.           record++;                          /* increment record counter */
  69.           if (error != 0) {                  /* error = 0 if no file  */
  70.              compare = strncmp(dis_buffer, input, strlen(input));
  71.              if (compare == 0) {             /* compare = 0 if match  */
  72.                 dis_record(record);          /* display record        */
  73.                 crt(20, 1, "Do You Want To Modify This Record (y/n) ");
  74.                 again = answer();            /* yes = 1, no = 0       */
  75.                 if (again == 1)
  76.                     modify(record);          /* yes -> modify record  */
  77.                 again = 0;                   /* don't continue search */
  78.              }
  79.           } else      
  80.              again = 0;                      /* error -> no repeat    */
  81.        } while (again == 1);                 /* continue search       */
  82.        crt(20,1,"Do You Want To Search For Another User (y/n) ");
  83.        again = answer();                     /* find another          */
  84.     } while (again == 1);                    /* 1 = again, 0 = end    */
  85. }
  86.  
  87. /* routine to update the users record with the new information */
  88.  
  89. modify(record)
  90. int record;
  91. {
  92.     char flag, modified, input[200];
  93.  
  94.     do {
  95.        crt(20,1,"Parameter To Modify (0 = End)"); /* get parameter to mod */
  96.        flag = getchar() - '0';  /* read keyboard & convert to decimal */ 
  97.        switch (flag) {
  98.        case 0: break;           /* if zero then we are done! */
  99.        case 1: crt(20,1,"New User Name: ");
  100.                gets(&input);
  101.                update(0, 31, input);
  102.                modified = 1;
  103.                break;
  104.        case 2: crt(20,1,"New User Password: ");
  105.                gets(&input);
  106.                update(31, 15, input);
  107.                modified = 1;
  108.                break;
  109.        case 3: crt(20,1,"New User Security Level: ");
  110.                gets(&input);
  111.                update_word(46, input);
  112.                modified = 1;
  113.                break;
  114.        case 4: crt(20,1,"New User City, State: ");
  115.                gets(&input);
  116.                update(62, 24, input);
  117.                modified = 1;
  118.                break;
  119.        case 5: crt(20, 1, "New User Machine: ");
  120.                gets(&input);
  121.                update(86, 19, input);
  122.                modified = 1;
  123.                break;
  124.        case 6: crt(20, 1, "New User Downloads: ");
  125.                gets(&input);
  126.                update_word(122, input);
  127.                modified = 1;
  128.                break;
  129.        case 7: crt(20, 1, "New User Uploads: ");
  130.                gets(&input);
  131.                update_word(124, input);
  132.                modified = 1;
  133.                break;
  134.        }
  135.     if (flag != 0)
  136.        dis_record(record);    /* display modified data */
  137.     } while ( flag != 0);
  138.     crt(20, 1, "OK, To Update USERS file (y/n) ");
  139.     flag = answer();
  140.     if (flag == 1)
  141.        update_users_file(record); /* if not ok, then return nothing changed */
  142. }
  143.  
  144. /* routine to write updated users record to the disk */
  145.  
  146. update_users_file(record)
  147. int record;
  148. {
  149.     long file_offset;
  150.  
  151.     rewind(fp);
  152.     file_offset = --record * 128;
  153.     fseek(fp, file_offset, 0);
  154.     write(fp, dis_buffer, 128);
  155. }
  156.  
  157. /* routine to update a series of ascii characters in an array */
  158.  
  159. update(offset, count, string)
  160. char offset, count, string[];
  161. {
  162.     char cnt;
  163.     for (cnt = 0; cnt < count; cnt++)
  164.         dis_buffer[offset+cnt] = ' ';
  165.     count = strlen(string);
  166.     cnvrt_string(string);
  167.     for (cnt = 0; cnt < count; cnt++)
  168.         dis_buffer[offset+cnt] = string[cnt];
  169. }
  170.  
  171. /* routine to update a integer stored as a two byte ascii string */
  172.  
  173. update_word(offset, string)
  174. char offset, string[];
  175. {
  176.     unsigned number;
  177.     number = atoi(string);
  178.     dis_buffer[offset] = number % 256;
  179.     dis_buffer[offset+1] = number / 256;
  180. }
  181.  
  182. /* sets cursor to row, col, and then displays a string */
  183.  
  184. crt(row, col, s)
  185. char row, col, s[];
  186. {
  187.     scr_rowcol(row, col);
  188.     scr_cls();
  189.     printf("%s", s);
  190. }
  191.  
  192. /* routine to interagate the keyboard. This routine will wait for a key
  193.    to be depressed. If any key other than a upper or lower case "Y" is
  194.    struck a zero will be returned else a one is returned. */
  195.  
  196. answer()
  197. {
  198.     char  c;
  199.     c = toupper(getchar());
  200.     if (c == 'Y')
  201.        return(1);
  202.     else
  203.        return(0);
  204. }
  205.  
  206. /* routine to convert ascii string to upper case letters */
  207.  
  208. cnvrt_string(s)
  209. char *s;
  210. {
  211.     do {
  212.        *s = toupper(*s);
  213.     } while(*s++);
  214. }
  215.  
  216. /* routine to read a users file record from the disk */
  217.  
  218. get_record()
  219. {
  220.     char cnt;
  221.     for (cnt = 0; cnt <= 128; cnt++)
  222.         dis_buffer[cnt] = 9;
  223.     error = fread(dis_buffer, 128, 1, fp);
  224. }
  225.  
  226. /* this routine takes data read from the users file and converts it to
  227.    variables that are more convenient for display */
  228.  
  229. convert_data()
  230. {
  231.     move_data(name, 0, 31);
  232.     move_data(password, 31, 15);
  233.     security = set_word(46);
  234.     op1 = set_word(48);
  235.     op2 = set_word(50);
  236.     op3 = set_word(52);
  237.     op4 = set_word(54);
  238.     op5 = set_word(56);
  239.     op6 = set_word(58);
  240.     op7 = set_word(60);
  241.     move_data(city_state, 62, 24);
  242.     move_data(machine, 86, 19);
  243.     move_data(time_date, 105, 14);
  244.     move_data(last_dir, 119, 3);
  245.     downloads = set_word(122);
  246.     uploads = set_word(124);
  247.     elapsed = set_word(126);
  248. }
  249.  
  250. /* moves string array data down (s[]) starting at position start by the
  251.    number passed in count */
  252.  
  253. move_data(s, start, count)
  254. char s[], start, count;
  255. {
  256.     char cnt;
  257.     for (cnt = 0; cnt < count; cnt++)
  258.         s[cnt] = dis_buffer[start+cnt];
  259.     s[++cnt] = '\0';
  260. }
  261.  
  262. /* changes to consecutive string bytes into an integer */
  263.     
  264. set_word(offset)
  265. char offset;
  266. {
  267.     char hi, lo;
  268.     hi = dis_buffer[offset+1];
  269.     lo = dis_buffer[offset];
  270.     return(hi * 256 + lo);
  271. }
  272.  
  273. /* display data record in the standard format */
  274.  
  275. dis_record(record)
  276. int record;
  277. {
  278.     convert_data();                  /* converts data record for display */
  279.     crt(1,3,"Record Number =======> ");
  280.     printf("%d\n", record);
  281.     printf("1. User Name ===========> %s\n", name);
  282.     printf("2. Password ============> %s\n", password);
  283.     printf("3. Security Level ======> %d\n", security);
  284.     printf("   Times On System =====> %d\n", op1);
  285.     printf("   Last Message Read ===> %d\n", op2);
  286.     printf("   Line Feeds ==========> \0");
  287.     print_mode(op3, "ON", "OFF");
  288.     printf("   Right Margin At =====> %d\n", op4);
  289.     printf("   Prompt Bell =========> \0");
  290.     print_mode(op5, "ON", "OFF");
  291.     printf("   Expert/Novice Mode ==> \0");
  292.     print_mode(op6, "NOVICE", "EXPERT");
  293.     printf("   Page Length =========> %d\n", op7);
  294.     printf("4. City, State =========> %s\n", city_state);
  295.     printf("5. Users Machine =======> %s\n", machine);
  296.     printf("   Time/Date Last On ===> %s\n", time_date);
  297.     printf("   Last Directory List => \0");
  298.     printf("%02d/%02d/%02d\n", last_dir[1], last_dir[2], last_dir[0]);
  299.     printf("6. Downloads ===========> %d\n", downloads);
  300.     printf("7. Uploads =============> %d\n", uploads);
  301.     printf("   Elapsed Time (min) ==> %d\n", elapsed);
  302. }
  303.  
  304. /* print_mode will print string passed to s1 if flag is zero, else
  305.    the string passed in s2 will be printed */
  306.  
  307.  
  308. print_mode(flag, s1, s2)
  309. int flag;
  310. char s1[], s2[];
  311. {
  312.     if (flag == 0)
  313.        printf("%s\n", s1);
  314.     else
  315.        printf("%s\n", s2);
  316. }
  317.  
  318. /* routine to display opening screen */
  319.  
  320. screen()
  321. {
  322.     char c;
  323.  
  324.     scr_clr();              /* clear screen */
  325.     center(7, "CLEANUP -- By William K. Davies");
  326.     center(9, "A USERS file maintenance utility -- DSOFT");
  327.     center(10, "Version 1.1  11/25/1984");
  328.     center(11, "Data (714)989-6706");
  329.     center(24, "Press Enter Key To Continue...");
  330.     do {
  331.        c = getchar();       /* wait for a carriage return */
  332.     } while ( c != 13);
  333. }
  334.  
  335. /* routine to display a string centered on the screen at the specified row */
  336.  
  337. center(row, s)
  338. char row, s[];
  339. {
  340.     char l;
  341.  
  342.     l = (80 - strlen(s)) / 2;    /* calculate column */
  343.     scr_rowcol(row, l);          /* set cursor postion */
  344.     printf("%s\0", s);
  345. }